/**
 * 
 */
package com.fox.webapp.persistent.impl;

import static org.springframework.data.mongodb.core.query.Criteria.where;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import com.fox.webapp.domain.Person;
import com.fox.webapp.domain.WithdrawRecord;
import com.fox.webapp.persistent.custom.WithdrawRecordRepositoryCustom;

/**
 * @author Yockii
 *
 */
public class WithdrawRecordRepositoryImpl implements
		WithdrawRecordRepositoryCustom {
	@Autowired
	private MongoTemplate mongoTemplate;
	/* (non-Javadoc)
	 * @see com.fox.webapp.persistent.custom.WithdrawRecordRepositoryCustom#findWithCondition(com.fox.webapp.domain.WithdrawRecord, org.springframework.data.domain.PageRequest)
	 */
	@Override
	public Page<WithdrawRecord> findWithCondition(WithdrawRecord condition,
			Pageable pageRequest) {
		Query query = new Query();
		Person p = condition.getPerson();
		if(p != null && p.getId() != null){
			query.addCriteria(where("person").is(p));
		}
		long total = mongoTemplate.count(query, WithdrawRecord.class);
		query.skip(pageRequest.getOffset());
		query.limit(pageRequest.getPageSize());
		List<WithdrawRecord> wrList = mongoTemplate.find(query, WithdrawRecord.class);
		Page<WithdrawRecord> pg = new PageImpl<WithdrawRecord>(wrList, pageRequest, total);
		return pg;
	}
	@Override
	public int countWithCondition(WithdrawRecord condition) {
		Query query = new Query();
		Person p = condition.getPerson();
		if(p != null && p.getId() != null){
			query.addCriteria(where("person").is(p));
		}
		return (int)mongoTemplate.count(query, WithdrawRecord.class);
	}

}
